package com.yang.contentaudit.service.impl;

import cn.hutool.core.io.file.FileReader;
import com.baidu.aip.contentcensor.AipContentCensor;
import com.yang.contentaudit.constants.AuditResult;
import com.yang.contentaudit.dto.AuditResponse;
import com.yang.contentaudit.service.ContentAuditService;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.util.List;


/**
 * @author: Billyang
 * @date: 2024/9/30 17:13
 */
@Slf4j
public class BaiduContentAuditService implements ContentAuditService {

    private AipContentCensor client;

    private final String successMessage = "合规";

    public BaiduContentAuditService(String appId, String apiKey, String secretKey) {
        client = new AipContentCensor(appId, apiKey, secretKey);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "logback-spring.xml");
    }

    @Override
    public AuditResponse auditImg(byte[] bytes) {
        JSONObject result = client.imageCensorUserDefined(bytes, null);
        log.info("result:{}", result.toString());
        if (AuditResult.SUCCESS == result.getInt("conclusionType")) {
            return AuditResponse.success();
        }

        if (AuditResult.AUDIT_ERROR == result.getInt("conclusionType")) {
            return AuditResponse.review("图片审核失败");
        }

        JSONObject data = result.getJSONArray("data").getJSONObject(0);
        if (AuditResult.REVIEW == result.getInt("conclusionType")) {
            return AuditResponse.review(data.getString("msg"));
        }
        return AuditResponse.fail(data.getString("msg"));
    }

    @Override
    public AuditResponse auditImgList(List<byte[]> imageList) {
        for (byte[] bytes : imageList) {
            AuditResponse resp = this.auditImg(bytes);
            //只要有任意一个失败，那就直接返回
            if(!resp.getCode().equals(AuditResult.SUCCESS)){
                return resp;
            }
        }
        return AuditResponse.success();
    }

    @Override
    public AuditResponse auditText(String text) {
        JSONObject result = client.textCensorUserDefined(text);
        log.info("result:{}", result.toString());
        if (AuditResult.SUCCESS == result.getInt("conclusionType")) {
            return AuditResponse.success();
        }
        if (AuditResult.AUDIT_ERROR == result.getInt("conclusionType")) {
            return AuditResponse.review("文本审核失败");
        }

        JSONObject data = result.getJSONArray("data").getJSONObject(0);
        if (AuditResult.REVIEW == result.getInt("conclusionType")) {
            return AuditResponse.review(data.getString("msg"));
        }
        return AuditResponse.fail(data.getString("msg"));
    }

    public static void main(String[] args) throws Exception {
        String appId = "115745179";
        String apiKey = "eEwETcTZ6DRi1kpyJmsPxvFo";
        String secretKey = "pQPdbYHPJKcGEJNHSFLqsTTrTMoxrChh";
        BaiduContentAuditService baiduContentAuditService = new BaiduContentAuditService(appId, apiKey, secretKey);
        FileReader fileReader = new FileReader("F:\\BaiduNetdiskDownload\\mini\\P\\6.jpg");
        byte[] bytes = fileReader.readBytes();
        AuditResponse imgResp = baiduContentAuditService.auditImg(bytes);
        AuditResponse textResp = baiduContentAuditService.auditText("擦边女");
        System.out.println(imgResp);
        System.out.println(textResp);
    }
}
